<?php
/**
 * Created by PhpStorm
 * User: whh
 * Date: 2022/11/24
 * Time: 14:53
 */

namespace app\backstage\controller;


use app\backstage\model\QuickAppPush;
use think\Controller;

class Push extends Controller
{
    protected $middleware = [
        'VerifyJwt' => ['except' => []],
        'VerifyPower' => ['except' => []],
    ];

    /**
     * 加桌推送
     * @return mixed
     * @Author: whh
     */
    public function saveDesktopPush()
    {
        $role = request()->_role;
        $user = request()->_user;
        if ($role->alias == 'channel' || $role->alias == 'agent' || $role->alias == 'eb') {
            $quickApps = $user->apps;
        } else {
            $quickApps = \app\common\model\QuickApp::all();
        }
        $this->assign('quickApps', $quickApps);
        $this->assign('role', $role);
        return $this->fetch();
    }

    /**
     * 定时推送
     * @return mixed
     * @Author: whh
     */
    public function timedPush()
    {
        $role = request()->_role;
        $user = request()->_user;
        if ($role->alias == 'channel' || $role->alias == 'agent' || $role->alias == 'eb') {
            $quickApps = $user->apps;
        } else {
            $quickApps = \app\common\model\QuickApp::all();
        }
        $this->assign('quickApps', $quickApps);
        $this->assign('role', $role);
        return $this->fetch();
    }

    /**
     * 推送列表
     * @return \think\response\Json
     * @Author: whh
     */
    public function list()
    {
        $title = trim(input('title'));
        $quick_id = input('quick_id');
        $channel_id = trim(input('channel_id'));
        $page = input('page', 1);
        $limit = input('limit', 10);
        $push_type = input('push_type');
        $where = [];
        if ($push_type != '') {
            $where[] = ['push_type', '=', $push_type];
        }
        if ($title != '') {
            $where[] = ['title', 'like', "%{$title}%"];
        }
        if ($quick_id != '') {
            $where[] = ['quick_id', '=', $quick_id];
        }
        if ($channel_id != '') {
            $where[] = ['channel_id', '=', $channel_id];
        }
        $role = request()->_role;
        $user = request()->_user;
        if ($role->alias == 'agent') {
            $userModel = new \app\backstage\model\ManagerUser();
            $channelIds = $userModel->sonUsersIds($user->id);
            $where[] = ['channel_id', 'in', $channelIds];
            $where[] = ['channel_id', '<>', 0];
        }
        if ($role->alias == 'channel') {
            $where[] = ['channel_id', '=', $user->id];
        }

        $orderBy = "id desc";
        if ($push_type == 2) {
            $orderBy = "send_time desc,id desc";
        }
        $list = QuickAppPush::getList($where, '*', $orderBy, $page, $limit, ['channel', 'quick_app']);
        $count = QuickAppPush::getCount($where);
        return sucJson($list, $count);
    }


    /**
     * 删除推送
     * @throws \Exception
     * @Author: whh
     */
    public function delPush()
    {
        $ids = input('ids/a');
        $role = request()->_role;
        $user = request()->_user;
        $where[] = ['id', 'in', $ids];
        if ($role->alias == 'agent') {
            $userModel = new \app\backstage\model\ManagerUser();
            $channelIds = $userModel->sonUsersIds($user->id);
            $where[] = ['channel_id', 'in', $channelIds];
        }
        if ($role->alias == 'channel') {
            $where[] = ['channel_id', '=', $user->id];
        }
        QuickAppPush::where($where)->delete();
        $this->success();
    }
}